/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis.logicgame.actions;

import com.jme.math.Matrix3f;
import com.jme.math.Vector3f;
import edu.ieslaferreria.creditsintesis.logicgame.MobilObject;

/**
 * RotateLeftAction turns the node to the left (while
 * traveling forward).
 * @author Mark Powell
 *
 */
public class RotateLeftAction implements Action<ProcessMobileObjectStateEvent> {
    //temporary variables to handle rotation
    private static final Matrix3f incr = new Matrix3f();
    private static final Matrix3f tempMa = new Matrix3f();
    private static final Matrix3f tempMb = new Matrix3f();

    //we are using +Y as our up
    private Vector3f upAxis = new Vector3f(0,1,0);

    /**
     * turn the node by its turning speed. If the node is traveling
     * backwards, swap direction.
     */
    public boolean performAction(ProcessMobileObjectStateEvent evt) {
        MobilObject node = (MobilObject) evt.getObjectWithStates();
        //we want to turn differently depending on which direction we are traveling in.
        if(node.getLinealVelocity()< 0) {
            incr.fromAngleNormalAxis(-node.getRotationVelocity() * evt.getTime(), upAxis);
        } else {
            incr.fromAngleNormalAxis(node.getRotationVelocity() * evt.getTime(), upAxis);
        }
        node.getLocalRotation().fromRotationMatrix(
                incr.mult(node.getLocalRotation().toRotationMatrix(tempMa),
                        tempMb));
        node.getLocalRotation().normalize();

        return true;
    }
}

